<?php

/*
 * 	Codificado: 02-Noviembre-2011, 01:23
	Estado: Ajustado_Modelo_Actual(X) Funcionando()
 */

class Personas_Model extends CI_Model
{
	private  $table = 'Personas';

	/** Utility Methods **/
	function _required($required, $data)
	{
		foreach($required as $field)
		if(!isset($data[$field])) return false;
			
		return true;
	}

	function Insertar($option = array()) {
		if ($this->_required(array( 'NumeroIdentificacion','IdTipo','Nombres','Apellidos','Sexo','FechaNacimiento' ,
						'Ciudad','Pais','Telefono','Correo','FacultadDepencia','Programa'), $options))
		return FALSE ;

		$this->db->insert($this->table, $options);
		return $this->db->insert_id();

	}

	function Editar($options = array()) {
		if(!$this->_required(array('NumeroIdentificacion'),$options))
		return FALSE;

		unset($options['submit'] );

		if(isset($options['IdTipo']))
		$this->db->set('IdTipo', $options['IdTipo']);

		if(isset($options['Nombres']))
		$this->db->set('Nombres', $options['Nombres']);

		if(isset($options['Apellidos']))
		$this->db->set('Apellidos', $options['Apellidos']);

		$this->db->where('NumeroIdentificacion', $options['NumeroIdentificacion']);
		$this->db->update($this->table, $options);

		return $this->db->affected_rows();
	}

	function Eliminar($options = array()) {

		$this->db->where('NumeroIdentificacion', $options['NumeroIdentificacion']);
		$this->db->delete($this->table);

		if($data['error'] = $this->db->_error_message());

		return $data;

	}

	function Ver($options = array()) {

		$this->db->where('NumeroIdentificacion', $options['NumeroIdentificacion']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}

	function Listar($options = array()) {

		$this->db->select('NumeroIdentificacion,Nombres,Apellidos,IdTipo' );
		if(isset($options['limit']) && isset($options['offset']))
		$this->db->limit($options['limit'], $options['offset']);
		else if(isset($options['limit']))
		$this->db->limit($options['limit']);
			
		$query = $this->db->get($this->table);
		if(isset($options['count']))
		return $query->num_rows();
			
		return $query->result_array();
	}

	function PersonasDropDown()
	{
			
		// Qualification
		$this->db->select( 'NumeroIdentificacion, Nombres, Apellidos');
		$this->db->order_by('NumeroIdentificacion', 'ASC');

		$query = $this->db->get($this->table);
			
		return $query->result_array();
	}

	function BuscarPorIdTipo($options = array()) {
		if ($this->_required(array('IdTipo'), $options)) return FALSE ;

		$this->db->where('IdTipo', $options['IdTipo']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}
	function BuscarPorSexo($options = array()) {
		if ($this->_required(array('Sexo'), $options)) return FALSE ;

		$this->db->where('Sexo', $options['Sexo']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}

	function BuscarPorFacultadDependencia($options = array()) {
		if ($this->_required(array('FacultadDepencia'), $options)) return FALSE ;

		$this->db->where('FacultadDepencia', $options['FacultadDepencia']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}

	function BuscarPorPrograma($options = array()) {
		if ($this->_required(array('Programa'), $options)) return FALSE ;

		$this->db->where('Programa', $options['Programa']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}

	function BuscarPorAnoFinalizacionEstudios($options = array()) {
		if ($this->_required(array('AnoFinalizacionEstudios'), $options)) return FALSE ;

		$this->db->where('AnoFinalizacionEstudios', $options['AnoFinalizacionEstudios']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}
}

?>